from rest_framework import serializers

from apps.goods.models import SPUSpecification, SKUSpecification, SpecificationOption


class SpuModel(serializers.ModelSerializer):
    # 关联嵌套返回spu表的商品名
    spu = serializers.StringRelatedField(read_only=True)
    # 返回关联spu的id值
    spu_id = serializers.IntegerField()
    class Meta:
        model=SPUSpecification
        fields='__all__'



class OptionsRealted(serializers.ModelSerializer):

    class Meta:
        model=SpecificationOption
        fields=['id','value']




class SPUSpecificationModel(serializers.ModelSerializer):
    spu=serializers.StringRelatedField(read_only=True)
    spu_id=serializers.IntegerField()
    options=OptionsRealted(read_only=True,many=True)
    class Meta:
        model=SPUSpecification
        fields='__all__'
    # [
    #     {
    #         "id": "规格id",
    #         "name": "规格名称",
    #         "spu": "SPU商品名称",
    #         "spu_id": "SPU商品id",
    #         "options": [
    #             {
    #                 "id": "选项id",
    #                 "name": "选项名称"
    #             },
    #             ...
    #         ]
    #     },
    #     ...
    # ]



class SpecsOptionsModelSer(serializers.ModelSerializer):
    spec=serializers.StringRelatedField(read_only=True)
    spec_id=serializers.IntegerField()
    class Meta:
        model=SpecificationOption
        fields='__all__'

class SpecsOptionsAll(serializers.ModelSerializer):
    # spec = serializers.StringRelatedField(read_only=True)
    # spec_id = serializers.IntegerField()
    class Meta:
        model=SPUSpecification
        fields=['id','name']
